package com.wingy.myspinnerdemo;

import android.graphics.drawable.Animatable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class SpinnerDemoActivity extends AppCompatActivity {

    private static final String[] countriesStr = {"广州", "从化", "武汉", "汕头"};
    private TextView myTextview;
    private Spinner mySpinner;
    private ArrayAdapter arrayAdapter;
    Animation animation;
    private EditText editText;
    private Button button_add;
    private Button button_remove;
    private List allCountries;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spinner_demo);

        allCountries = new ArrayList();
        for(int i = 0; i<countriesStr.length; i++) {
            allCountries.add(countriesStr[i]);
        }

        myTextview = (TextView) findViewById(R.id.myTextView);
        mySpinner = (Spinner) findViewById(R.id.spinner);
        editText = (EditText) findViewById(R.id.myEditText);
        button_add = (Button) findViewById(R.id.myButton_add);
        button_remove = (Button) findViewById(R.id.myButton_remove);

        arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, allCountries);
        arrayAdapter.setDropDownViewResource(R.layout.dropdown);
        mySpinner.setAdapter(arrayAdapter);

        button_add.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View v) {
                String newCounty = editText.getText().toString();
                for(int i = 0; i<arrayAdapter.getCount(); i++) {
                    if(newCounty.equals(arrayAdapter.getItem(i))) {
                        Toast.makeText(SpinnerDemoActivity.this, "已经有了哟~",Toast.LENGTH_SHORT).show();
                        return;
                    }
                }
                if (!newCounty.equals("")) {
                    arrayAdapter.add(newCounty);
                    int position = arrayAdapter.getPosition(newCounty);
                    mySpinner.setSelection(position);
                    editText.setText("");
                }
            }
        });

        button_remove.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View v) {
                if(mySpinner.getSelectedItem() != null) {
                    arrayAdapter.remove(mySpinner.getSelectedItem().toString());
                    editText.setText("");
                    if(arrayAdapter.getCount() == 0) myTextview.setText("");
                }
            }
        });

        mySpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                myTextview.setText(parent.getSelectedItem().toString());
                parent.setVisibility(View.VISIBLE);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        animation = AnimationUtils.loadAnimation(this, R.anim.myainm);

        mySpinner.setOnTouchListener(new Spinner.OnTouchListener(){
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                v.startAnimation(animation);
                v.setVisibility(View.VISIBLE);
                return false;
            }
        });

        mySpinner.setOnFocusChangeListener(new Spinner.OnFocusChangeListener(){
            @Override
            public void onFocusChange(View v, boolean hasFocus) {

            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.spinner_demo, menu);
        return true;
    }
}


